﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;

namespace METU_CMT.Business
{
    public class CourseTA : Course
    {
        public CourseTA(int courseId):base(courseId)
        {
            
        }

        protected CourseTA()
        {

        }

        public void AddAnnouncement(Announcement announcement)
        {
            int? _announcementId;

            CourseTableAdapter _courseTableAdapter = new CourseTableAdapter();
            _courseTableAdapter.InsertAnnouncement(courseId, announcement, out _announcementId);
            announcement.AnnouncementId = ( int ) _announcementId;
            
            announcements.Add( announcement );
        }

        public void AddEvent(Event e)
        {
            schedule.Add(e);

            CourseTableAdapter _courseTableAdapter = new CourseTableAdapter();
            _courseTableAdapter.InsertEvent(courseId, e);
        }

        public void AddNote(Note note)
        {
            lectureNotes.Add(note);

            CourseTableAdapter _courseTableAdapter = new CourseTableAdapter();
            _courseTableAdapter.InsertLectureNote( courseId, note );
        }

        public void AddReviewer(ReviewTeam reviewerTeam) 
        {
            reviewTeams.Add(reviewerTeam);

            CourseTableAdapter _courseTableAdapter = new CourseTableAdapter();
            _courseTableAdapter.InsertReviewerTeam(reviewerTeam);
        }

        public void AddTeam(Team team)
        {
            teams.Add(team);

            CourseTableAdapter _courseTableAdapter = new CourseTableAdapter();
            _courseTableAdapter.InsertTeam(courseId, team);
        }

        public void DeleteAnnouncement(int deletedAnnouncementId)
        {
            announcements.RemoveAll( announcement => announcement.AnnouncementId == deletedAnnouncementId );

            CourseTableAdapter _courseTableAdapter = new CourseTableAdapter();
            _courseTableAdapter.RemoveAnnouncement(deletedAnnouncementId);
        }

        public void DeleteEvent(int e)
        {
            schedule.RemoveAll( _event => _event.EventId == e );

            CourseTableAdapter _courseTableAdapter = new CourseTableAdapter();
            _courseTableAdapter.RemoveEvent(courseId, e);
        }

        public void DeleteNote(int deletedNoteId)
        {
            lectureNotes.RemoveAll(note => note.NoteId == deletedNoteId);

            CourseTableAdapter _courseTableAdapter = new CourseTableAdapter();
            _courseTableAdapter.RemoveLectureNote(deletedNoteId);
        }

        public void DeleteReviewer(int reviewTeamId)
        {
            reviewTeams.RemoveAll( reviewTeam => reviewTeam.ReviewTeamId == reviewTeamId );

            CourseTableAdapter _courseTableAdapter = new CourseTableAdapter();
            _courseTableAdapter.RemoveReviewerTeam(reviewTeamId);
        }

        public void DeleteTeam(Team team)
        {
            teams.RemoveAll( _team => _team.TeamId == team.TeamId );

            CourseTableAdapter _courseTableAdapter = new CourseTableAdapter();
            _courseTableAdapter.RemoveTeam(courseId, team);
        }

        public void UpdateAnnouncement(Announcement updatedAnnouncement)
        {
            announcements.RemoveAll( announcement => announcement.AnnouncementId == updatedAnnouncement.AnnouncementId );
            announcements.Add( updatedAnnouncement );

            CourseTableAdapter _courseTableAdapter = new CourseTableAdapter();
            _courseTableAdapter.UpdateAnnouncement( courseId, updatedAnnouncement );
        }

        public List<Student> GetUnassignedStudents()
        {
            List< Student > _students = students.Where(
                student => teams.Any( team => team.Members.Any( _student => _student.UserId == student.UserId ) ) == false ).
                ToList( );
            return _students;
        }

        public void UpdateEvent(int oldEvent, Event newEvent)
        {
            schedule.Find( _event => _event.EventId == oldEvent ).Description = newEvent.Description;
            schedule.Find(_event => _event.EventId == oldEvent).DueDate = newEvent.DueDate;
            schedule.Find(_event => _event.EventId == oldEvent).SubmissionExist  = newEvent.SubmissionExist;

            CourseTableAdapter _courseTableAdapter = new CourseTableAdapter();
            _courseTableAdapter.UpdateEvent(courseId, oldEvent, newEvent);
        }

        public void GoToEventWork(Event @event)
        {
            HttpContext.Current.Session["CurrentEvent"] = @event;
            HttpContext.Current.Response.Redirect("~/ListDownloadSubmittedDocsScreen.aspx");
        }

        public override void GoToCourseMainPage(HttpResponse response)
        {
            HttpContext.Current.Session["MenuItem2"] = "~/TeachingAssistantCourseMainScreen.aspx";
            response.Redirect("~/TeachingAssistantCourseMainScreen.aspx");
        }
    }
}